import { getContextOrSetDefault } from './contextUtil'
import { EventAbility } from '../event/EventAbility'

const contextEventKey = '__event_bus__'

function getContextEvent () {
  return getContextOrSetDefault(contextEventKey, () => new EventAbility())
}

export const ContextEventNames = {
  onReachBottom:'onReachBottom',
  onPageScroll:'onPageScroll',
  onPullDownRefresh:'onPullDownRefresh',
}
const ContextEvent = {
  on (eventName, handler) {
    getContextEvent().on(eventName, handler)
  },
  off (eventName, handler) {
    getContextEvent().off(eventName, handler)
  },
  emit (eventName, ...args) {
    getContextEvent().emit(eventName, ...args)
  }
}

export default ContextEvent